<template>
  <div>
    <el-dialog
      title="添加部门"
      :visible="dialogFormVisible"
      :show-close="false"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          label="部门名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="部门编码"
          :label-width="formLabelWidth"
          prop="code"
        >
          <el-input v-model="form.code" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="部门负责人"
          :label-width="formLabelWidth"
          prop="manager"
        >
          <el-select v-model="form.manager" placeholder="请选择活动区域">
            <el-option
              v-for="item in manager"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="部门介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="closeDia">确 定</el-button>
      </div>
      <div slot="title" class="title">
        <p style="color: #fff; margin-left: 300px">
          {{ isEdit ? '编辑部门' : '添加部门' }}
        </p>
        <span
          class="el-icon-close"
          @click="$emit('update:dialogFormVisible', false)"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartmentsApi, updateDepartmentsApi } from '@/api/departments'
export default {
  name: 'AddDia',
  props: {
    manager: {
      type: Array,
      default: () => []
    },
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: ''
    },
    departItem: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      formLabelWidth: '120px',
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {
    departItem: {
      immediate: true,
      handler(val) {
        if (this.isEdit) {
          this.form = val
        } else {
          this.form = {
            code: '',
            introduce: '',
            manager: '',
            name: '',
            pid: ''
          }
        }
      }
    }
  },
  created() {
    // this.form = this.departItem
  },
  methods: {
    // 当对话框关闭时触发的事件
    close() {
      this.$emit('update:dialogFormVisible', false)
    },
    closeDia() {
      this.$refs.form.validate((valid) => {
        if (!valid) return this.$message.error('请输入内容')
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      this.form.pid = this.id
      await addDepartmentsApi(this.form)
      // this.$emit('update:dialogFormVisible', false)
      // 成功后触发父级
      this.$emit('success')
    },
    async doEdit() {
      this.form.pid = this.id
      await updateDepartmentsApi(this.form)
      // this.$emit('update:dialogFormVisible', false)
      // 成功后触发父级
      this.$emit('success')
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  text-align: center;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 30px;
  p {
    font-size: 20px;
  }
}
</style>
